// pages/distribution/distribution_center.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    uid: '',
    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
    nickName: '点击显示微信头像',
    if_register: false,
    target_uid: '',
    target_point: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init_data(options)
    this.get_my_point()
  },

  confirm_gift() {
    if (this.data.target_uid == '') {
      util.showToast("请输入正确的id号码")
      return
    }

    if (this.data.target_uid == this.data.uid) {
      util.showToast("请输入正确的id号码")
      return
    }

    if (this.data.target_uid <= 0) {
      util.showToast("请输入正确的id号码")
      return
    }

    let params = {
      from_uid: this.data.uid,
      to_uid: this.data.target_uid,
      amount: this.data.target_point,
    }

    util.showLoading('正在赠送')

    request.post(api.transpoint, params).then(res => {
      util.hideLoading()
      util.showToast('赠送成功')
      this.onLoad()
    })
  },

  get_my_point() {
    let params = {
      uid: this.data.uid
    }
    request.post(api.myinfo, params).then(res => {
      this.setData({
        my_point: res.point
      })
    })
  },

  init_data(options) {
    let uid = util.getAppValue(App.globalData.uid)
    let eshop_id = App.globalData.eshop_id
    if (!uid) {
      uid = 1
    }
    this.setData({
      if_register: App.globalData.if_register,
      uid,
      options,
      eshop_id,
    })
    let avatarUrl = App.globalData.userInfo.avatarUrl
    let nickName = App.globalData.userInfo.nickName
    if (avatarUrl) {
      this.setData({
        avatarUrl,
        nickName
      })
    }
  },

  input_target_uid(e) {
    this.setData({
      target_uid: e.detail.value
    })
  },

  input_target_point(e) {
    this.setData({
      target_point: e.detail.value
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})